<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	cfc('core')->checklogin();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['status']!=100){
		if($_RQ['status']==5){
			$params['status']=array(9,5);
		}elseif($_RQ['status']==10){
			$params['status']=9;
			$params['ispay']=0;
		}else{
			$params['status']=$_RQ['status'];
		}
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_fuwu_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	if($return['list']){
		foreach($return['list'] as &$v){
			if($v['cuid'] && $v['cuid']!=$_SESSION['uid']){
				$v['cel']=1;
			}
			$v['worker']=pdo_get('jy_fuwu_worker',array('id'=>$v['worker_id']),array('name','mobile','sex','avatar','mid'));
			$v['worker']['ordersum']=pdo_count('jy_fuwu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'worker_id'=>$v['worker_id']),'id');
			$v['worker']['avatar']=tomedia($v['worker']['avatar']);
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
			$v['serve']=pdo_get('jy_fuwu_serve',array('id'=>$v['serve_id']));
		}
	}
	$return['type']=$this->order_status;
	$return['params']=array('status'=>$_RQ['status']?:100);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$order=pdo_get('jy_fuwu_order',array('id'=>$id));
		pdo_delete('jy_fuwu_order',array('id'=>$id));	
	}
	exi('删除成功！','','jy_fuwu/order/list/');

}
if($_RQ['op']=='cel'){

	$id=intval($_RQ['id']);
	
	if($id){
		$order=pdo_get('jy_fuwu_order',array('id'=>$id));
		if($order['status']==1){
			pdo_update('jy_fuwu_order',array('status'=>2,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}else{
			pdo_update('jy_fuwu_order',array('status'=>3,'cuid'=>$_SESSION['uid']),array('id'=>$id));	
		}
	}
	
	exi('已申请取消,请联系对方确认','','jy_fuwu/order/list/');

}
if($_RQ['op']=='dcel'){
	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_cancel($id);
	}
	exi('已确定取消','','jy_fuwu/order/list/');
}
if($_RQ['op']=='call'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_fuwu_order',array('id'=>$id));
	pdo_update('jy_fuwu_order',array('createtime'=>TIMESTAMP),array('id'=>$id));
	$this->model('worker')->syspai($order);
	$params=array(
			'mid'=>$order['mid'],
			'message'=>'您重新呼叫了一次服务！',
	);
	$this->model('notice')->add_msg($params);
	exi('呼叫成功！');
}
if($_RQ['op']=='done'){

	$id=intval($_RQ['id']);
	
	if($id){
		$order=pdo_get('jy_fuwu_order',array('id'=>$id));
		if($order['status']!=9 || !$order['ispay']){
			exi('未服务完成/未支付的订单无法完成','error');
		}
		$set=$this->plugin_setting('setting');
		$bili=$set['credit']?$set['credit']/100:0.2;
		$credit=round($order['price']*$bili,2);
		pdo_update('core_members',array('credit2 -='=>$credit),array('id'=>$order['worker_id']));
		pdo_update('jy_fuwu_order',array('status'=>5),array('id'=>$id));
		$worker_id=pdo_getcolumn('jy_fuwu_order',array('id'=>$id),'worker_id');
		pdo_update('jy_fuwu_worker',array('busy'=>1),array('id'=>$worker_id));
		
	}
	exi('已完成','','jy_fuwu/order/list/');

}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','list');
	}
	$return['item']=pdo_get('jy_fuwu_order',array('id'=>$id));
	$return['item']['worker']=pdo_get('jy_fuwu_worker',array('id'=>$return['item']['worker_id']),array('name','mobile','sex','avatar','mid'));
	$return['item']['worker']['avatar']=tomedia($return['item']['worker']['avatar']);
	$return['item']['worker']['ordersum']=pdo_count('jy_fuwu_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(5,9),'worker_id'=>$return['item']['worker_id']),'id');
	$return['item']['createtime']=date('Y-m-d H:i:s',$return['item']['createtime']);
	exi($return);
}
if($_RQ['op']=='sendwxapp_neworder'){
	$id = intval($_RQ['id']);
	$this->model('notice')->notice_create2buyer($id);
	exi('success');	
}
if($_RQ['op']=='pay'){
	$order_id = intval($_RQ['id']);
	$order=$this->pdo_get('order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	$payid=$order['pay_id'];
	if(!$payid){
		$params=array(
			'id'=>$order_id,
			'title'=>'服务支付',
			'fee'=>$order['price'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_fuwu/order/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_fuwu/order/',
			'error_url'=>$error_url?:'jy_fuwu/order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('order',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_fuwu/order/');
	}
	$order=pdo_get('jy_fuwu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_fuwu/order/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_fuwu/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$this->model('order')->order_pay($order['id'],$params);
	exi('提交成功!');
}
?>